package pe.demo.ejemplodnie;

import java.io.IOException;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import javax.security.cert.X509Certificate;
import sun.security.mscapi.SunMSCAPI;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main(String[] args) throws
            KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
            UnrecoverableEntryException, InvalidKeyException, SignatureException, NoSuchProviderException
    {
        System.out.println( "Hello World!" );
        
        try
        {
//         ejemplo1();
            ejemplo2();
        }
        catch(UnrecoverableKeyException uke)
        {
            System.out.println("error : " + uke.getMessage());
        }
    }
    
    static void ejemplo1() throws
            KeyStoreException, IOException, NoSuchAlgorithmException,
            CertificateException, NoSuchProviderException {
        
        Provider[] providers = Security.getProviders();
        for (int i = 0; i < providers.length; i++)  System.out.println("# " + i + " : " + providers[i].toString());
        
//        SunMSCAPI providerMSCAPI = new SunMSCAPI();
//        Security.addProvider(providerMSCAPI);
        
//        for (int i = 0; i < providers.length; i++)  System.out.println("# " + i + " : " + providers[i].toString());
        KeyStore ks = KeyStore.getInstance("Windows-MY");//, "SunMSCAPI");
        // here check if smart card is inserted
        ks.load(null,null);
        
        Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            System.out.println("Alias: "+ alias);
            if (ks.isCertificateEntry(alias)){
                System.out.println(">>> is cert");
            } else {
                System.out.println(">>> is key");
            }
        }
    }
    
    static void ejemplo2() throws
            KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableEntryException, InvalidKeyException, SignatureException {
        
        SunMSCAPI providerMSCAPI = new SunMSCAPI();
        Security.addProvider(providerMSCAPI);
        KeyStore ks = KeyStore.getInstance("Windows-MY");
        ks.load(null,null);
        
        Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            if (ks.isKeyEntry(alias)) {
                System.out.println("Alias: "+ alias);
//           /*
                KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry)ks.getEntry(alias, null);
                PrivateKey privKey = entry.getPrivateKey();
                //PrivateKey privKey = (PrivateKey) ks.getKey(alias, null);
                System.out.println("privKey.getAlgorithm(): "+ privKey.getAlgorithm());
                
                Signature sig = Signature.getInstance("SHA1withRSA");
                sig.initSign(privKey);
                sig.update("Hello World".getBytes());
                
                byte[] signatureValue = sig.sign();
                System.out.println("signatureValue: "+ signatureValue.length);
                break;
//                   */
            }
        }
    }
}

